// ==UserScript==
// @name         微信公众平台Cookie获取器
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  获取微信公众平台的Cookie和URL，并下载为JSON文件
// @author       George Cai
// @match        https://mp.weixin.qq.com/*
// @grant        GM.xmlHttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // 创建一个按钮
    function createButton() {
        const button = document.createElement('button');
        button.innerHTML = '获取Cookie和Token';
        button.style.position = 'fixed';
        button.style.top = '10px';
        button.style.right = '10px';
        button.style.zIndex = '9999';
        button.style.padding = '10px';
        button.style.backgroundColor = '#07C160';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.borderRadius = '4px';
        button.style.cursor = 'pointer';
        
        button.addEventListener('click', getCookieAndToken);
        document.body.appendChild(button);
    }

    // 从URL中提取token
    function getTokenFromUrl(url) {
        const urlObj = new URL(url);
        return urlObj.searchParams.get('token') || '';
    }

    // 显示成功提示
    function showSuccessMessage() {
        const message = document.createElement('div');
        message.innerHTML = '获取成功！请点击下方"搜索公众号"按钮继续';
        message.style.position = 'fixed';
        message.style.top = '10px';
        message.style.right = '200px';
        message.style.padding = '10px';
        message.style.backgroundColor = 'rgba(7, 193, 96, 0.9)';
        message.style.color = 'white';
        message.style.borderRadius = '4px';
        message.style.zIndex = '9999';
        message.style.opacity = '1';
        message.style.transition = 'opacity 0.5s ease-in-out';
        message.style.whiteSpace = 'nowrap';
        message.style.boxShadow = '0 2px 4px rgba(0,0,0,0.2)';
        
        document.body.appendChild(message);

        // 3秒后淡出消失
        setTimeout(() => {
            message.style.opacity = '0';
            setTimeout(() => {
                document.body.removeChild(message);
            }, 500);
        }, 3000);
    }

    // 获取cookie和token
    function getCookieAndToken() {
        const currentUrl = window.location.href;

        // 使用GM.xmlHttpRequest获取响应头中的Set-Cookie
        GM.xmlHttpRequest({
            method: 'GET',
            url: currentUrl,
            headers: {
                'Cache-Control': 'no-cache',
                'Pragma': 'no-cache'
            },
            onload: function(response) {
                // 获取所有响应头
                const headers = response.responseHeaders.split('\n');
                const setCookies = [];
                
                // 提取所有Set-Cookie头
                headers.forEach(header => {
                    const [name, ...values] = header.split(':');
                    if (name.toLowerCase().trim() === 'set-cookie') {
                        setCookies.push(values.join(':').trim());
                    }
                });

                const info = {
                    token: getTokenFromUrl(currentUrl),
                    raw_set_cookies: setCookies.join('; ')
                };

                // 保存到localStorage
                localStorage.setItem('wechat_auth_data', JSON.stringify(info));

                // 显示成功提示
                showSuccessMessage();
            },
            onerror: function(error) {
                console.error('获取Cookie失败:', error);
                alert('获取Cookie失败，请查看控制台了解详情');
            }
        });
    }

    // 等待页面加载完成后创建按钮
    window.addEventListener('load', createButton);
})(); 